//
//  YTCarAddViewModel.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/8.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTCarAddViewModel.h"
#import "YTDriverCarServices.h"
#import "YTDriverTeamServices.h"

@interface YTCarAddViewModel ()
@property (strong, nonatomic, readwrite) RACCommand *nextStepCommand;
@property (strong, nonatomic, readwrite) RACCommand *submitCommand;
/** 车辆id，在车辆添加完成以后，服务器会返回车辆id，如果在前面有选择关联司导，这里需要把选择的司导和这个车辆关联在一起*/
@property (copy, nonatomic) NSString *car_id;
@end

@implementation YTCarAddViewModel

- (void)initialize {
    [super initialize];
    
    // 一坨校验
    id (^notEmpty)(NSString *str) = ^id(NSString *str) { // 非空
        return @(str.length != 0);
    };
    
    RACSignal *versionSingal = [RACObserve(self, carBand) map:notEmpty]; // 车辆品牌
    RACSignal *numSignal = [RACObserve(self, carNum) map:notEmpty]; // 车牌
    RACSignal *typeSignal = [RACObserve(self, carVersion) map:notEmpty]; // 载客规格
    RACSignal *dateSignal = [RACObserve(self, carDate) map:notEmpty]; // 出厂日期
    
    id (^notEmpty1)(UIImage *image) = ^id(UIImage *image) { // 图片非空
        return @(image != nil);
    };
    RACSignal *image1Signal = [RACObserve(self, image1) map:notEmpty1];
    RACSignal *image2Signal = [RACObserve(self, image2) map:notEmpty1];
    RACSignal *image3Signal = [RACObserve(self, image3) map:notEmpty1];
    
    RACSignal *enableSignal = [RACSignal combineLatest:@[versionSingal, numSignal,
                                                         typeSignal, dateSignal,
                                                         image1Signal, image2Signal,
                                                         image3Signal]
                                                reduce:^id(NSNumber *version, NSNumber *num,
                                                           NSNumber *type, NSNumber *date,
                                                           NSNumber *image1, NSNumber *image2,
                                                           NSNumber *image3){
                                                    return @([version boolValue] && [num boolValue] && [type boolValue] && [date boolValue] &&
                                                    [image1 boolValue] && [image2 boolValue] && [image3 boolValue]);
    }];
    
    // 下一步
    _nextStepCommand = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
        return [RACSignal empty];
    }];
    
    // 提交
    id (^selectedDate)(NSString *str) = ^id(NSString *str) { // 非空
        return @(str.length > 5);
    };
    RACSignal *driverDate = [RACObserve(self, carDrivingLicenseTime) map:selectedDate];
    RACSignal *inspectDate = [RACObserve(self, carInspectexpTime) map:selectedDate];
    RACSignal *insuranceDate = [RACObserve(self, carInsuranceTime) map:selectedDate];
    RACSignal *driverImage = [RACObserve(self, drivingLicenseImage) map:notEmpty1];
    RACSignal *inspectImage = [RACObserve(self, carInspectexpImage) map:notEmpty1];
    RACSignal *insuranceImage = [RACObserve(self, insuranceImage) map:notEmpty1];
    RACSignal *submitEnable = [RACSignal combineLatest:@[driverDate, inspectDate, insuranceDate,
                                                         driverImage, inspectImage, insuranceImage]
                                                reduce:^id(NSNumber *driver, NSNumber *inspect, NSNumber *insurance,
                                                           NSNumber *image1, NSNumber *image2, NSNumber *image3) {
        return @(driver.boolValue && inspect.boolValue && insurance.boolValue && image1.boolValue && image2.boolValue && image3.boolValue);
    }];
    
    @weakify(self);
    _submitCommand = [[RACCommand alloc] initWithEnabled:submitEnable signalBlock:^RACSignal *(id input) {
        @strongify(self);
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        // 前一页数据
        dict[@"carnumber"] = self.carNum;
        dict[@"brandVersion"] = self.carBand;
        dict[@"seatType"] = self.carVersion;
        dict[@"carColorId"] = self.carColor;
        dict[@"ownerName"] = self.carOwnerName;
        dict[@"isSelf"] = [NSString stringWithFormat:@"%zd", self.isOwner];
        dict[@"carImage1"] = [UIImage yt_imageToString:self.image1];
        dict[@"carImage2"] = [UIImage yt_imageToString:self.image2];
        dict[@"carImage3"] = [UIImage yt_imageToString:self.image3];
        
        // 后一页
        dict[@"drivingLicenseImage"] = [UIImage yt_imageToString:self.drivingLicenseImage];
        dict[@"insuranceImage"] = [UIImage yt_imageToString:self.insuranceImage];
        dict[@"carInspectexpImage"] = [UIImage yt_imageToString:self.carInspectexpImage];
        dict[@"carDrivingLicenseTime"] = self.carDrivingLicenseTime;
        dict[@"carInspectexpTime"] = self.carInspectexpTime;
        dict[@"carInsexpTime"] = self.carInsuranceTime;
        dict[@"carRegTime"] = self.carDate;
        
        return [[YTDriverCarServices addCarWithData:dict] doNext:^(NSDictionary *response) {
            NSDictionary *dict = response[@"responseData"];
            if (self.driver_id) { // 如果有id，则绑定车辆和司导的关系
                NSMutableDictionary *data = [NSMutableDictionary dictionary];
                data[@"driverId"] = self.driver_id;
                data[@"carId"] = dict[@"carId"];
                [YTDriverTeamServices addCarToDriver:data];
            }
        }];
    }];
}

@end
